home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / samba.idb / usr / samba / src / source / nmbd_winsproxy.c.z / nmbd_winsproxy.c
Encoding:
C/C++ Source or Header  |  1998-10-28  |  7.9 KB  |  219 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    NBT netbios routines and daemon - version 2
  5.  
  6.    Copyright (C) Jeremy Allison 1994-1998
  7.  
  8.    This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2 of the License, or
  11.    (at your option) any later version.
  12.    
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.    
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.    
  22. */
  23.  
  24. #include "includes.h"
  25.  
  26. extern int DEBUGLEVEL;
  27.  
  28. /****************************************************************************
  29. Function called when the name lookup succeeded.
  30. ****************************************************************************/
  31.  
  32. static void wins_proxy_name_query_request_success( struct subnet_record *subrec,
  33.                         struct userdata_struct *userdata,
  34.                         struct nmb_name *nmbname, struct in_addr ip, struct res_rec *rrec)
  35. {
  36.   struct packet_struct *original_packet;
  37.   struct subnet_record *orig_broadcast_subnet;
  38.   struct name_record *namerec;
  39.   uint16 nb_flags;
  40.   int num_ips;
  41.   int i;
  42.   int ttl = 3600; /* By default one hour in the cache. */
  43.   struct in_addr *iplist;
  44.  
  45.   /* Extract the original packet and the original broadcast subnet from
  46.      the userdata. */
  47.  
  48.   memcpy( (char *)&orig_broadcast_subnet, userdata->data, sizeof(struct subnet_record *) );
  49.   memcpy( (char *)&original_packet, &userdata->data[sizeof(struct subnet_record *)],
  50.           sizeof(struct packet_struct *) );
  51.  
  52.   nb_flags = get_nb_flags( rrec->rdata );
  53.  
  54.   num_ips = rrec->rdlength / 6;
  55.   if(num_ips == 0)
  56.   {
  57.     DEBUG(0,("wins_proxy_name_query_request_success: Invalid number of IP records (0) \
  58. returned for name %s.\n", namestr(nmbname) ));
  59.     return;
  60.   }
  61.  
  62.   if(num_ips == 1)
  63.     iplist = &ip;
  64.   else
  65.   {
  66.     if((iplist = (struct in_addr *)malloc( num_ips * sizeof(struct in_addr) )) == NULL)
  67.     {
  68.       DEBUG(0,("wins_proxy_name_query_request_success: malloc fail !\n"));
  69.       return;
  70.     }
  71.  
  72.     for(i = 0; i < num_ips; i++)
  73.       putip( (char *)&iplist[i], (char *)&rrec->rdata[ (i*6) + 2]);
  74.   }
  75.  
  76.   /* Add the queried name to the original subnet as a WINS_PROXY_NAME. */
  77.  
  78.   if(rrec == PERMANENT_TTL)
  79.     ttl = lp_max_ttl();
  80.  
  81.   namerec = add_name_to_subnet( orig_broadcast_subnet, nmbname->name, nmbname->name_type,
  82.                          nb_flags, ttl, WINS_PROXY_NAME, num_ips, iplist);
  83.  
  84.   if(iplist != &ip)
  85.     free((char *)iplist);
  86.  
  87.   /*
  88.    * Check that none of the IP addresses we are returning is on the
  89.    * same broadcast subnet as the original requesting packet. If it
  90.    * is then don't reply (although we still need to add the name
  91.    * to the cache) as the actual machine will be replying also
  92.    * and we don't want two replies to a broadcast query.
  93.    */
  94.  
  95.   if(namerec && original_packet->packet.nmb.header.nm_flags.bcast)
  96.   {
  97.     for( i = 0; i < namerec->num_ips; i++)
  98.     {
  99.       if(same_net( namerec->ip[i], orig_broadcast_subnet->myip, orig_broadcast_subnet->mask_ip ))
  100.       {
  101.         DEBUG(5,("wins_proxy_name_query_request_success: name %s is a WINS proxy name and is also \
  102. on the same subnet (%s) as the requestor. Not replying.\n", 
  103.                   namestr(&namerec->name), orig_broadcast_subnet->subnet_name ));
  104.         return;
  105.       }
  106.     }
  107.   }
  108.  
  109.   /* Finally reply to the original name query. */
  110.   reply_netbios_packet(original_packet,                /* Packet to reply to. */
  111.                        0,                              /* Result code. */
  112.                        NMB_QUERY,                      /* nmbd type code. */
  113.                        NMB_NAME_QUERY_OPCODE,          /* opcode. */
  114.                        ttl,                            /* ttl. */
  115.                        rrec->rdata,                    /* data to send. */
  116.                        rrec->rdlength);                /* data length. */
  117. }
  118.  
  119. /****************************************************************************
  120. Function called when the name lookup failed.
  121. ****************************************************************************/
  122.  
  123. static void wins_proxy_name_query_request_fail(struct subnet_record *subrec,
  124.                                     struct response_record *rrec,
  125.                                     struct nmb_name *question_name, int fail_code)
  126. {
  127.   DEBUG(4,("wins_proxy_name_query_request_fail: WINS server returned error code %d for lookup \
  128. of name %s.\n", fail_code, namestr(question_name) ));
  129. }
  130.  
  131. /****************************************************************************
  132. Function to make a deep copy of the userdata we will need when the WINS
  133. proxy query returns.
  134. ****************************************************************************/
  135.  
  136. static struct userdata_struct *wins_proxy_userdata_copy_fn(struct userdata_struct *userdata)
  137. {
  138.   struct packet_struct *p, *copy_of_p;
  139.   struct userdata_struct *new_userdata = 
  140.         (struct userdata_struct *)malloc( userdata->userdata_len );
  141.  
  142.   if(new_userdata == NULL)
  143.     return NULL;
  144.  
  145.   new_userdata->copy_fn = userdata->copy_fn;
  146.   new_userdata->free_fn = userdata->free_fn;
  147.   new_userdata->userdata_len = userdata->userdata_len;
  148.  
  149.   /* Copy the subnet_record pointer. */
  150.   memcpy( new_userdata->data, userdata->data, sizeof(struct subnet_record *) );
  151.  
  152.   /* Extract the pointer to the packet struct */
  153.   memcpy((char *)&p, &userdata->data[sizeof(struct subnet_record *)],
  154.          sizeof(struct packet_struct *) );
  155.  
  156.   /* Do a deep copy of the packet. */
  157.   if((copy_of_p = copy_packet(p)) == NULL)
  158.   {
  159.     free((char *)new_userdata);
  160.     return NULL;
  161.   }
  162.  
  163.   /* Lock the copy. */
  164.   copy_of_p->locked = True;
  165.  
  166.   memcpy( &new_userdata->data[sizeof(struct subnet_record *)], (char *)©_of_p,
  167.           sizeof(struct packet_struct *) );
  168.  
  169.   return new_userdata;
  170. }
  171.  
  172. /****************************************************************************
  173. Function to free the deep copy of the userdata we used when the WINS
  174. proxy query returned.
  175. ****************************************************************************/
  176.  
  177. static void wins_proxy_userdata_free_fn(struct userdata_struct *userdata)
  178. {
  179.   struct packet_struct *p;
  180.  
  181.   /* Extract the pointer to the packet struct */
  182.   memcpy((char *)&p, &userdata->data[sizeof(struct subnet_record *)],
  183.          sizeof(struct packet_struct *));
  184.  
  185.   /* Unlock the packet. */
  186.   p->locked = False;
  187.  
  188.   free_packet(p);
  189.   free((char *)userdata);
  190. }
  191.  
  192. /****************************************************************************
  193.  Make a WINS query on behalf of a broadcast client name query request.
  194. ****************************************************************************/
  195.  
  196. void make_wins_proxy_name_query_request( struct subnet_record *subrec, 
  197.                                          struct packet_struct *incoming_packet,
  198.                                          struct nmb_name *question_name)
  199. {
  200.   char ud[sizeof(struct userdata_struct) + sizeof(struct subrec *) + 
  201.           sizeof(struct packet_struct *)];
  202.   struct userdata_struct *userdata = (struct userdata_struct *)ud;
  203.  
  204.   bzero(ud, sizeof(ud));
  205.  
  206.   userdata->copy_fn = wins_proxy_userdata_copy_fn;
  207.   userdata->free_fn = wins_proxy_userdata_free_fn;
  208.   userdata->userdata_len = sizeof(ud);
  209.   memcpy( userdata->data, (char *)&subrec, sizeof(struct subnet_record *));
  210.   memcpy( &userdata->data[sizeof(struct subnet_record *)], (char *)&incoming_packet,
  211.           sizeof(struct packet_struct *));
  212.  
  213.   /* Now use the unicast subnet to query the name with the WINS server. */
  214.   query_name( unicast_subnet, question_name->name, question_name->name_type,
  215.               wins_proxy_name_query_request_success,
  216.               wins_proxy_name_query_request_fail,
  217.               userdata);
  218. }
  219.